package westh.ilib.struts.action.myTagCloud;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.mule.extras.client.MuleClient;
import org.mule.umo.UMOMessage;
import westh.config.Config;
import westh.config.Config;
import westh.ilib.bean.tagBean.TagSys;
import westh.ilib.service.tagClouds.TagCloudsService;
import westh.log.YyError;
import westh.log.YyLog;

/**
 * @author ChaiLinglu
 * 
 * @ModifyDate 2010-09-14
 * @modifier ChaiLinglu
 * @content refine
 * 
 */
public class TagCloudSettingAction extends Action {
	private final static Log log = LogFactory.getLog(TagCloudSettingAction.class);
	//get clouds Ip address
	String localhostUrl = Config.get("localhostUrl");
	String remoteUrl = Config.get("remoteUrl");
	
	public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) {
		DynaActionForm f = (DynaActionForm) form;
		//get data from page
		String userId=request.getRemoteUser();
		userId="ebb70a6400e64872ad24fe5a9da30c2c";///////////////////////////////////////tempTest
		String dateStart = f.getString("dateStart");
		String dateEnd = f.getString("dateEnd");
		String tagNum = f.getString("dispnum");
		String module=f.getString("module");
		
		String redirect="";
		if(module.equals("myTagCloud")){
			redirect="../getMyTagClouds.do";
		}
		if(module.equals("sysTagCloud")){
			redirect="../getSysTagClouds.do";
		}
		String area=f.getString("area");//////////////////////////////////////
		area="";//////////////////////////////////////
		
		//create input json for CDO
		JSONObject resultObject = new JSONObject();
		JSONObject inputJson=new JSONObject();
		resultObject.put("userId", userId);
		resultObject.put("queryDateStart", dateStart);
		resultObject.put("queryDateEnd", dateEnd);
		resultObject.put("tagDispNum", tagNum);
		resultObject.put("module", module);
		resultObject.put("area", area);
		inputJson.put("methodName", "saveTagSetting");
		inputJson.put("jsonStr", resultObject);
		String inputStr = inputJson.toString();
		
		// call webService  --  CDO
		String outString = "";
		try {
			MuleClient clientD = new MuleClient();
			UMOMessage umo = clientD.send("axis:http://"+localhostUrl+":4381/LIBJson/iDlibJson?method=rulesControl",inputStr, null);
			outString = (String) umo.getPayload();
			log.debug("saveTagSetting:Output:CDO:"+outString);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

		
		String retCode=""; //return code
		String retMsg="";//return message
		String returnStr="";//

		JSONObject jsonOut= JSONObject.fromObject(outString);
		retCode = jsonOut.getString("retCode");
		if (retCode.equals("0")) {
			retMsg = jsonOut.getString("retMsg");
			request.setAttribute("info", retMsg);
			returnStr = "succ";
			
			//create input json for ULASCloud
			JSONObject json=new JSONObject();
			JSONObject jsonObj=new JSONObject();
			jsonObj.put("userId", userId);
			jsonObj.put("time", TagCloudsService.getCurrentTime());
			jsonObj.put("rscId", " ");
			jsonObj.put("doing", TagCloudsService.getUserSetStr());
			json.put("methodName", "saveTagLog");//rules control
			json.put("jsonStr", jsonObj);//rules control
			
			//call ULASCloud
			westh.log.YyLog.rulesSave(request,json.toString());

		} else {
			YyError.show(retCode);
			request.setAttribute("info", "设置失败！");
			request.setAttribute("redirect", redirect);
			returnStr = "fail";
		}
		request.setAttribute("info", "设置成功！");
		request.setAttribute("redirect", redirect);
		return mapping.findForward(returnStr);
	}
}
